package com.IO.stream;

import java.io.*;

/***
 * @ClassName: BufferedDemo
 * @Description: 字节缓冲流读取文件
 * @Auther: 送一一块二向箔
 * @Date: 21:48 2022/2/6
 * @version : V1.0
 */
public class BufferedDemo {
    public static void main(String[] args) throws IOException {
        bufferedReader();
        bufferedWrite();
    }

    public static void bufferedReader() throws IOException {
        // 1 创建BufferedInputStream
        FileInputStream fis = new FileInputStream("d:\\aaa.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        int data = 0;
        while ((data = bis.read())!=-1) {
            System.out.print((char) data);
        }
        fis.close();
    }

    public static void bufferedWrite() throws IOException{
        FileOutputStream fos = new FileOutputStream("d:\\ccc.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        for (int i = 0; i < 10; i++) {
            //写入8k缓冲区
            bos.write("Hello World\n".getBytes());
            //缓冲写入内存
            bos.flush();
        }
        //关闭流会自动刷新缓冲
        bos.close();
    }
}
